package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/inner/rpc/cart")
public class CartRpcController {
    @Autowired
    private CartService cartService;

    @GetMapping("/add/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum,
                            Request.Options options) {

        //redis中里面保存 key-value，跨层传递数据（1方法参数传递，2线程绑定机制）
        //1添加购物车

        String cartKey = cartService.determinCartKey();

        SkuInfo skuInfo = cartService.addCart(skuId, skuNum, cartKey);

        return Result.ok(skuInfo);
    }

    /**
     * 删除所选中的商品
     * @return
     */
    @DeleteMapping("/deleteChecked")
    public Result deleteChecked(){

        String cartKey = cartService.determinCartKey();

        cartService.deleteChecked(cartKey);

        return Result.ok();
    }

    /**
     * 获取所选中的商品
     * @return
     */
    @GetMapping("/checkeds")
    Result<List<CartInfo>> getChecked(){
        String cartKey = cartService.determinCartKey();
        List<CartInfo> infos=cartService.getChecked(cartKey);
        return  Result.ok(infos);

    }
}
